<template>
  <div class="bg">
    <div class="tabbar">
      <div class="title">EduPanda</div>
    </div>
    <div class="container">
      <div class="content">
        <div class="desc">
          <div>留学生奖学金咨询平台</div>
        </div>
        <div class="login">
          <el-menu
            :default-active="activeIndex"
            class="el-menu-demo"
            mode="horizontal"
            active-text-color="#0077C2"
            style="margin: 0px 30px; margin-top: 50px"
          >
            <el-menu-item index="1" @click="activeForm = true"
              >EduPanda账号</el-menu-item
            >
            <el-menu-item index="2" @click="activeForm = false"
              >邮箱注册</el-menu-item
            >
          </el-menu>
          <el-form
            style="padding: 30px; padding-bottom: 0px"
            :model="form"
            v-if="activeForm === true"
            label-width="60px"
          >
            <el-form-item label="账号" prop="username">
              <el-input
                :prefix-icon="User"
                placeholder="请输入账号"
                v-model="form.username"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                :prefix-icon="Lock"
                placeholder="请输入密码"
                v-model="form.password"
              ></el-input>
            </el-form-item>
            <el-button
              color="#0077c2"
              style="width: 100%; height: 40px"
              @click="register"
              :loading="loading"
              >立即注册</el-button
            >
          </el-form>
          <el-form
            style="padding: 30px; padding-bottom: 0px"
            :model="emailForm"
            label-width="60px"
            v-else
            :rules="rules"
            ref="EmailRegForm"
          >
            <el-form-item label="邮箱" prop="email">
              <el-input
                :prefix-icon="Message"
                placeholder="请输入邮箱账号"
                v-model="emailForm.email"
                type="email"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                :prefix-icon="Lock"
                placeholder="请输入密码"
                v-model="emailForm.password"
              ></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="captcha">
              <el-input
                :prefix-icon="InfoFilled"
                placeholder="请输入验证码"
                v-model="emailForm.captcha"
                style="width: 65%"
              ></el-input>
              <el-button
                type="primary"
                style="width: 35%; font-size: 12px"
                @click="getCaptcha"
                :disabled="ifGet"
                >{{
                  ifGet ? `请在${countDown}s后重新获取` : "获取验证码"
                }}</el-button
              >
            </el-form-item>
            <el-button
              color="#0077c2"
              style="width: 100%; height: 40px"
              @click="register"
              :loading="loading"
              >立即注册</el-button
            >
          </el-form>
          <div class="warning" style="margin-top: 10px">
            <div class="goLogin" @click="goLogin">
              <p>已有帐号?</p>
              <p style="color: #0077c2; cursor: pointer; margin-left: 6px">
                立即登录
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElNotification } from "element-plus";
import type { formData, emailFormData } from "@/api/user/type";
import { User, Lock, Message, InfoFilled } from "@element-plus/icons-vue";
import { reqGetCode, reqEmailReg } from "@/api/user";
let activeIndex = ref<string>("1");
let activeForm = ref<boolean>(true);
let form = ref<formData>({
  username: "",
  password: "",
});
let emailForm = ref<emailFormData>({
  email: "",
  password: "",
  captcha: "",
});
let loading = ref<boolean>(false);
const $router = useRouter();
let ifGet = ref<boolean>(false);
let countDown = ref<number>(60);
let EmailRegForm = ref();
const goLogin = () => {
  $router.push("login");
};
const register = async () => {
  await EmailRegForm.value.validate();
  loading.value = true;
  try {
    await reqEmailReg(emailForm.value);
    ElNotification({
      type: "success",
      title: "注册成功!",
      message: "即将访问登录页",
    });
    $router.push("login");
  } catch (error) {
    ElNotification({
      type: "error",
      title: "邮箱或密码有误!",
      message: (error as Error).message,
    });
  } finally {
    loading.value = false;
  }
};
const validatorCaptcha = (rule: any, value: any, callback: any) => {
  // rule：校验规则对象 -- value：表单元素文本内容
  // callback：符合条件->放行通过 不符合条件->注入错误提示信息
  if (value.length === 6) {
    callback();
  } else {
    callback(new Error("验证码长度为6位"));
  }
};
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 3) {
    callback();
  } else {
    callback(new Error("密码长度至少3位"));
  }
};
const rules = {
  email: [
    {
      trigger: "change",
      type: "email",
      message: "请输入正确的邮箱账号",
    },
  ],
  password: [
    {
      trigger: "change",
      validator: validatorPassword,
    },
  ],
  captcha: [
    {
      trigger: "change",
      validator: validatorCaptcha,
    },
  ],
};
const getCaptcha = async () => {
  const res = await reqGetCode(emailForm.value.email);
  if (res.success) {
    ifGet.value = true;
    let timer = setInterval(() => {
      countDown.value -= 1;
      if (countDown.value === 0) {
        clearInterval(timer);
        ifGet.value = false;
      }
    }, 1000);
  } else {
    ElMessage({
      type: "error",
      message: "获取验证码有误",
    });
  }
};
</script>

<style scoped lang="scss">
.bg {
  background-color: #f0f4fd;
  width: 100vw;
  height: 100vh;
  .tabbar {
    background-color: #fff;
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
    .title {
      width: 10%;
      height: 100%;
      margin-left: 8%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
  .container {
    width: 100%;
    margin-top: 100px;
    .content {
      width: 100%;
      display: flex;
      justify-content: space-evenly;
      .login:hover {
        box-shadow: 0 2px 40px 0 rgba(53, 113, 244, 0.355);
      }
      .login {
        background-color: #fff;
        width: 450px;
        height: 400px;
        border-radius: 6px;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
        transition: all 0.5s;
        .warning {
          width: 100%;
          padding: 30px;
          padding-top: 0px;
          display: flex;
          align-items: center;
          justify-content: center;
          .goLogin {
            display: flex;
            align-items: center;
            font-size: 13px;
          }
        }
      }
    }
  }
}
</style>